<template>
	<view class="main">
		<view class="login-top">
			<image src="http://xgr.oss-cn-hangzhou.aliyuncs.com/icon/icon/logo.png"></image>
		</view>
		<view style="margin-top: 100px; padding: 0 20rpx;">
			<button style=" background: #0575FF; color: #fff;" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" class="ft1">授权获取手机号码</button>
		</view>
		
	</view>
</template>

<script>
import Index from "@/common/index-model.js"
let index = new Index()
let that = null
export default {
	data() {
		return {
			istel:false,
			value:'',
			loginFrom:{
				// phone:'18066742510',
				phone:'',
				code:''
			},
			logBtn: true,
			codemsg:'获取验证码',
			sendDataShow: true,
			sendData: 60,
			phonecheckmsg:'',
			isphone:true,
			iscode:true,
			codecheckmsg:'',
			modetel:'',
			wxback:{
				code:'',
				type:'',
				uid:'',
				checkCode:''
				
			},
			// 用户凭证
			code: new String()
		}
	},
	async created() {
	  this.code = await this.getAppCode()
	},
	methods: {
		// ...mapActions(['Login']),
		// 获取code
		getAppCode() {
		  return new Promise((resolve, reject) => {
		    // #ifdef MP-WEIXIN
		    uni.login({
		      provider: 'weixin',
		      success(res) {
		        resolve(res.code)
		      },
		      fail(err) {
		        reject(err)
		      }
		    })
		    // #endif
		    // #ifdef MP-ALIPAY
		    my.getAuthCode({
		      scopes: 'auth_base',
		      success(res) {
		        resolve(res.authCode)
		      },
		      fail(err) {
		        reject(err)
		      }
		    })
		    // #endif
		  })
		},  
		getPhoneNumber (event) {
			if(event.detail.errMsg !== 'getPhoneNumber:ok') return
			uni.showToast({
			  title: '登录中',
			  icon: 'loading',
			  mask: true
			})
			// event.detail.code = this.code
			
			this.wxback.code=e.detail.code;
			console.log('this.wxback=======',this.wxback)
			index.getPublicMember(this.wxback,'/client/third/auth/bindMiniMobile','post',res=>{
				index.show_tips(res.data.message)
				uni.setStorageSync('token',res.data.result.token);
				// uni.setStorageSync('inviteUserId',res.data.result.inviteUserId);
				this.getHxUser();
				setTimeout(()=>{
					uni.navigateBack();
				},2000)
			})
		},
		getHxUser(){
			index.getPublicUserMember({},'/client/public/select/user','get',res=>{
				console.log(res);
				if(res.data.code==200){
					this.loginHx(res.data.result);
				}
			})
		},
		loginHx(data){
			var options = {
			    user: data.hxname,
			    pwd: data.hxPassword_,
			    appKey: this.$WebIM.config.appkey,
			    success: function (res) {
					// getApp().globalData.conn.open({
					// 	user: data.hxname,
					// 	accessToken: res.access_token,
					// });
					uni.setStorageSync('hxuser',data.hxname);
					uni.setStorageSync('hxpass',data.hxPassword_);
					uni.setStorageSync('hxtoken',res.access_token);
			    },
			    error: function(){
					
			    }
			};
			this.$WebIM.conn.open(options);
		},
		
	},
	onLoad(opt) {
		this.wxback.uid=opt.uid;
		this.wxback.checkCode=opt.checkCode;
	}
	
}
</script>

<style>
@import url("@/static/css/publics.css");	
@import url("@/static/css/login.css");		
</style>